package br.com.vertigo.sgci.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.persistence.EntityManager;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;

import br.com.vertigo.sgci.entity.Usuario;

@Name("usuarioConverter")
@org.jboss.seam.annotations.faces.Converter(forClass=Usuario.class)
public class UsuarioConverter implements Converter{
	
	@In
	private EntityManager entityManager;
	   
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
		try{
			Long id = Long.parseLong(value);
            if (id != null){
            	return entityManager.find(Usuario.class, id);
            }	
	    }catch (NumberFormatException e) {		    	
	    }
	    return null;      					
	}

	public String getAsString(FacesContext arg0, UIComponent arg1, Object object) {		
	    if (object instanceof Usuario){
	    	Usuario usuario = (Usuario) object;
	        return usuario.getIdUsuario().toString();
	    }else{
	         return null;
    	}		
	}

}
